package com.mf.morefun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mf.morefun.R;
import com.mf.morefun.bean.ArrangeItemBean;
import com.mf.morefun.listener.HomeWorkListListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ArrangeHomeWorkAdapter extends BaseAdapter {
    private Context mContext;
    private List<ArrangeItemBean> mArrangeItemList;
    private HomeWorkListListener listListener;

    public HomeWorkListListener getListListener() {
        return listListener;
    }

    public void setListListener(HomeWorkListListener listListener) {
        this.listListener = listListener;
    }

    public ArrangeHomeWorkAdapter(Context mContext, List<ArrangeItemBean> mArrangeItemList) {
        this.mContext = mContext;
        this.mArrangeItemList = mArrangeItemList;
    }

    @Override
    public int getCount() {
        return mArrangeItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return mArrangeItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (null == convertView) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_arrangeitem, null);
        } else {
            view = convertView;
        }
        if (null != mArrangeItemList && mArrangeItemList.size() > 0){
            ViewHolder holder = new ViewHolder(view);
            holder.tvArrange.setTag(position);
            holder.tvArrange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listListener.callBack((int) v.getTag(), 0);
                }
            });
            holder.tvDo.setTag(position);
            holder.tvDo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listListener.callBack((int) v.getTag(), 1);
                }
            });
            holder.tvName.setText(mArrangeItemList.get(position).getName());
            holder.tvStartDate.setText(mArrangeItemList.get(position).getStartdate());
            holder.tvEnddate.setText(mArrangeItemList.get(position).getEnddate());
            if (position == 0){
                holder.llBg.setBackgroundResource(R.color.nomalbg_color);
            }else if(position == (mArrangeItemList.size() - 1)){
                if ((position % 2) == 0){
                    holder.llBg.setBackgroundResource(R.color.nomalbg_color);
                }else {
                    holder.llBg.setBackgroundResource(R.drawable.white_bottomshape);
                }
            }else {
                if ((position % 2) == 0){
                    holder.llBg.setBackgroundResource(R.color.nomalbg_color);
                }else {
                    holder.llBg.setBackgroundResource(R.color.white);
                }
            }
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_startDate)
        TextView tvStartDate;
        @BindView(R.id.tv_enddate)
        TextView tvEnddate;
        @BindView(R.id.tv_arrange)
        TextView tvArrange;
        @BindView(R.id.tv_do)
        TextView tvDo;
        @BindView(R.id.ll_bg)
        LinearLayout llBg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
